package com.example.springbootjackson.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.http.HttpUtil;
import com.example.springbootjackson.model.Test;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @Author : Magic Jwz
 */
@RestController
@RequestMapping("/download")
@Slf4j
public class DownloadController {

    public static void main(String[] args) {
        String msg = "https://ovft-public-pictures.oss-cn-hangzhou.aliyuncs.com/ca5a2932595e4e9a8378e787a7266fe2.png";
        System.out.println(FileNameUtil.getSuffix(msg));
        System.out.println(FileNameUtil.mainName(msg));
    }

    public static String fileName(String url){
        return FileNameUtil.mainName(url) + FileNameUtil.getSuffix(url);
    }

    @GetMapping("test")
    public void download01(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String path = System.getProperty("java.io.tmpdir");
        String fileName = UUID.randomUUID().toString() + ".zip";
        //筛选存在于数据库的图片
        List<String> dbUrls = Arrays.asList("https://ovft-public-pictures.oss-cn-hangzhou.aliyuncs.com/ca5a2932595e4e9a8378e787a7266fe2.png",
                "https://ovft-public-pictures.oss-cn-hangzhou.aliyuncs.com/7efbc4f0b04a4f30b4adb45ca472c3e7.png");
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(path + fileName)));
        for (String url : dbUrls) {
            byte[] downloadBytes = HttpUtil.downloadBytes(url);
            InputStream input = new ByteArrayInputStream(downloadBytes);
            zos.putNextEntry(new ZipEntry( System.currentTimeMillis() + ".pdf"));
            //写入文件的方法，同上
            int size = 0;
            //设置读取数据缓存大小
            byte[] buffer = new byte[1024];
            while ((size = input.read(buffer)) > 0) {
                zos.write(buffer, 0, size);
            }
        }
        IOUtils.closeQuietly(zos);

//        byte[] bytes = readZipFileToByteArray(path + fileName);
//        String content = Base64.getEncoder().encodeToString(bytes);
//        Map map = new HashMap();
//        map.put("appname", "trade-operator");
//        map.put("filename", fileName + System.currentTimeMillis() + ".zip");
//        map.put("contents", content);
//        map.put("open", true);
//        ResultBody rb = null;
//        try {
//            rb = restTemplate.postForObject("http://SERVICE-FSS/file/upload", map, ResultBody.class);
//        } catch (Exception e) {
//            log.error("调用下载文件服务失败: {}", e);
//            throw new GlobalErrorInfoException(GlobalErrorInfoEnum.CALL_FSS_SERVICE_ERROR);
//        }
//        if (!rb.getCode().equals("200")) {
//            log.error("调用下载文件服务失败: {}", rb.getMsg());
//            throw new GlobalErrorInfoException(GlobalErrorInfoEnum.CALL_FSS_SERVICE_ERROR);
//        }
//        System.out.println(JSON.toJSONString(rb));


        FileUtil.del(path + fileName);
    }
}
